import React,{Component} from 'react';
import { connect } from 'dva';
import styles from './IndexPage.less';
import styles1 from './IndexPage.css';
import {getAgentsId} from '../../settings'
import { NavBar, Icon} from 'antd-mobile';
import { addSessionData } from '../../utils/localData'
import BulletinBoard from './bulletin_board/bulletin_board';
import Classification from './classification/classification';
import HomeBottom from './home_bottom/home_bottom';
import HomeBanner from './HomeBanner/HomeBanner.js';
import Loading from '../loading/loading'


@connect(({global}) => ({
    global
}))

export default class IndexPage extends Component {

  constructor() {
    super()
    this.state = {
      loading:true,
      isOk:false
    }
  }
  timer = '';
  render() {
    document.title = '首页'
    console.log(this.props.global)
    return (
      <div className={styles.home_all}>
        {this.state.loading?
        <Loading/>:
        <div>
          <div className={styles1.home_center}>
             <HomeBanner />
             <BulletinBoard />
             <Classification />
          </div>
          <HomeBottom type={1} userId={this.props.global.userId} businessId={this.props.global.BusinessInfo.businessId}/>
        </div>
        }
      </div>
    )
  }
  componentDidUpdate(){
    console.log(this.props)
    if(!this.state.isOk){
      if(this.props.global.BusinessInfo.agentsId){
        if(this.props.global.BusinessInfo.businessStatus===1){
          this.setState({loading:false,isOk:true})
        }else{
          this.props.history.replace({
            pathname:"/NoAgent",
          })
        }
      }else{
        this.props.history.replace({
          pathname:"/NoAgent",
        })
      }

    }
  }

  getMyToken(){
    window.YLJsBridge.call(
      'getToken',
      {},
      (data)=>{
        if(data.code===0){
          if(data.value===""){//用户未登陆
            window.YLJsBridge.call(
              'login',
              {},
              (data)=>{
                if(data.code===0){
                  addSessionData("xiyiToken",data.value)
                  const { dispatch } = this.props
                  console.log("==================调用登陆成功====================")
                  dispatch({
                      type: 'global/setBusinessId',
                      payload: {agentsId:getAgentsId()},
                  })
                }else{//登陆失败返回原生
                  window.YLJsBridge.call(
                    'exitApp',{},{})
                }
              })
          }else{//用户已经登陆  记录token
            addSessionData("xiyiToken",data.value)
            const { dispatch } = this.props
            dispatch({
                type: 'global/setBusinessId',
                payload: {agentsId:getAgentsId()},
            })
          }
        }else{
          alert("获取用户信息失败")
        }
      }
    )
  }

  componentDidMount(){
    // const { dispatch } = this.props
    // dispatch({
    //     type: 'global/setBusinessId',
    //     payload: {agentsId:getAgentsId()},
    // })


    this.timer = setInterval(() => {
      console.log("获取YLJsBridge。。。")
      if(window.YLJsBridge){
        clearInterval(this.timer)
        window.YLJsBridge.call(//获取代理商id
        'getAgentId',
        {},
        (data)=>{
          console.log(data)
          if(data.code==0){
              addSessionData("xiyiAgentsId",data.value)
              this.getMyToken()
            }else{
              alert("获取商家信息失败")
            }
          })
        }
    }, 500);
  }
}

